package com.controller
{
	
	
	import com.model.valueObjects.VoBranchMaster;
	import com.service.BranchMasterDetails;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class BranchMasterController extends AbstractController
	{
		import com.model.Model;
		
		[Inject]
		public var branchMasterDetails:BranchMasterDetails;
		
		
		//[PostConstruct]
		public function BranchMasterController()
		{
			super();	
		}
		
		[EventHandler(event="BranchMasterEvent.GET_ALL")]
		public function getAll():void{
			sh.executeServiceCall(branchMasterDetails.getAllBranches(),getAllResult, handleFault );
		}
		private function getAllResult(event:ResultEvent):void
		{
			model.branchMasterList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="BranchMasterEvent.ADD", properties="branchMastervo")]
		public function add(branchMastervo:VoBranchMaster):void
		{
			sh.executeServiceCall(branchMasterDetails.addNewBranch(branchMastervo), addResult, handleFault );	
		}
		private function addResult(event:ResultEvent):void
		{
			model.branchMasterList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("New Branch Added","Branch" );
		}
		
		[EventHandler(event="BranchMasterEvent.UPDATE", properties="branchMastervo")]
		public function updateCompany(branchMastervo:VoBranchMaster):void
		{
			sh.executeServiceCall(branchMasterDetails.editBranchDetails(branchMastervo), updateResult, handleFault );	
		}
		private function updateResult(event:ResultEvent):void
		{
			model.branchMasterList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("Details Updated ","Branch");
		}
		
		[EventHandler(event="BranchMasterEvent.DELETE", properties="serialnumber")]
		public function deleteR(serialnumber:Number):void
		{
			sh.executeServiceCall(branchMasterDetails.deleteBranchDetails(serialnumber), deleteResult, handleFault );	
		}
		private function deleteResult(event:ResultEvent):void
		{
			getAll();
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("Record Deleted ","Branch" );
		}

		[EventHandler(event="BranchMasterEvent.LOADCOMPANY")]
		public function loadCompanyName():void{
			sh.executeServiceCall(branchMasterDetails.getcompany(), getcompanyResult, handleFault );
		}
		private function getcompanyResult(event:ResultEvent):void{
			model.branchMasterComboList=event.result as ArrayCollection;
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}